﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Data.OleDb;
using System.Data.SqlClient;

namespace DavidDB.Reflection.DAL.Managers
{
    internal class DBHelper
    {

        internal static void Operator(PropertyInfo p, string ptype, object obj, object t)
        {
            if (obj != null)
            {
                if (obj.ToString() != "")
                {
                    switch (ptype)
                    {
                        case "System.String":
                            p.SetValue(t, obj, null);
                            break;
                        case "System.Int32":
                            p.SetValue(t, Convert.ToInt32(obj), null);
                            break;
                        case "System.Double":
                            p.SetValue(t, Convert.ToDouble(obj), null);
                            break;
                        case "System.Boolean":
                            p.SetValue(t, Convert.ToBoolean(obj), null);
                            break;
                        case "System.DateTime":
                            p.SetValue(t, Convert.ToDateTime(obj), null);
                            break;
                        case "System.Decimal":
                            p.SetValue(t, Convert.ToDecimal(obj), null);
                            break;
                    }
                }
                else
                {
                    p.SetValue(t, null, null);
                }
            }
            else
            {
                p.SetValue(t, null, null);
            }
        }

        /// <summary>
        /// 判断是否普通类型的可赋值的属性(读取数据时调用)
        /// </summary>
        /// <param name="p"></param>
        /// <param name="nameSpace"></param>
        /// <returns></returns>
        internal static bool NeedToParse(PropertyInfo p)
        {
            if (p.PropertyType.Namespace.Equals("System"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 判断字段是否可以被保存
        /// </summary>
        /// <param name="p"></param>
        /// <param name="nameSpace"></param>
        /// <param name="p0"></param>
        /// <returns></returns>
        internal static bool PropertyInfoCouldInsert(PropertyInfo p, PropertyInfo p0)
        {
            if (p.PropertyType.Namespace.Equals("System") && !p.Name.Equals(p0.Name))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        internal static int GetSystemNamespaceFieldCount(PropertyInfo[] infos)
        {
            int allCount = 0;
            foreach (PropertyInfo info in infos)
            {
                if (info.PropertyType.Namespace.Equals("System"))
                {
                    allCount++;
                }
            }
            //这里减去1 是减掉第一列ID
            return allCount - 1;
        }

        /// <summary>
        /// 判断字段是否可以被编辑
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="p0"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        internal static bool PropertyInfoCouldEdit(dynamic obj, PropertyInfo p0, PropertyInfo p)
        {
            if (!p.Name.Equals(p0.Name) && p.GetValue(obj, null) != null && p.PropertyType.Namespace.Equals("System"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 判断字段是否可以被当做拼写SQL语句的参数
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="p0"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        internal static bool ParamEnabled(dynamic obj, PropertyInfo p0, PropertyInfo p)
        {
            if (!p.Name.Equals(p0.Name) && p.GetValue(obj, null) != null && p.GetValue(obj, null).ToString() != int.MinValue.ToString() && !p.PropertyType.ToString().Equals("System.DateTime") && p.PropertyType.Namespace.Equals("System"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        internal static int GetTableCountByKeys(string tableName, string sqlparams, ADO.DbType DBType, string key, dynamic connection, dynamic trans, params dynamic[] param)
        {
            int cols = 0;
            string sql = string.Format("select count(" + key + ") from {0} where {1}", tableName, sqlparams);
            switch (DBType)
            {
                case ADO.DbType.MSSql:
                    cols = Convert.ToInt32(ADO.Tools.DbHelperSQL.GetSingle(connection, trans, sql, param));
                    break;
                //case ADO.DbType.MySql:
                //    cols = Convert.ToInt32(ADO.Tools.MysqlHelper.GetSingle(connection, trans, sql, param));
                //    break;
                case ADO.DbType.Access:
                    cols = Convert.ToInt32(ADO.Tools.AccessHelper.ExecuteScalar(connection, trans, sql, param));
                    break;
                case ADO.DbType.Oracle:
                    // cols = Convert.ToInt32(ADO.Tools.OracleHelper.GetSingle(sql, connection, trans, param));
                    break;
                default:
                    break;
            }
            return cols;
        }


        /// <summary>
        /// 得到表的记录总数
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        internal static int GetTableCount(string tableName, ADO.DbType DBType, string key, dynamic connection, dynamic trans)
        {
            int cols = 0;
            string sql = string.Format("select count(" + key + ") from {0}", tableName);
            switch (DBType)
            {
                case ADO.DbType.MSSql:
                    cols = Convert.ToInt32(ADO.Tools.DbHelperSQL.GetSingle(sql, connection, trans));
                    break;
                //case ADO.DbType.MySql:
                //    cols = Convert.ToInt32(ADO.Tools.MysqlHelper.GetSingle(sql, connection, trans));
                //    break;
                case ADO.DbType.Access:
                    cols = Convert.ToInt32(ADO.Tools.AccessHelper.ExecuteScalar(connection, trans, sql));
                    break;
                case ADO.DbType.Oracle:
                    cols = Convert.ToInt32(ADO.Tools.OracleHelper.GetSingle(sql, connection, trans));
                    break;
                default:
                    break;
            }
            return cols;
        }

        public static OleDbParameter GetParameter(PropertyInfo p)
        {
            switch (p.PropertyType.ToString())
            {
                case "System.DateTime":
                    return new OleDbParameter("@" + p.Name, OleDbType.Date);
                case "System.Int32":
                    return new OleDbParameter("@" + p.Name, OleDbType.Integer);
                case "System.Boolean":
                    return new OleDbParameter("@" + p.Name, OleDbType.Boolean);
                case "System.Double":
                    return new OleDbParameter("@" + p.Name, OleDbType.Decimal);
                case "System.Decimal":
                    return new OleDbParameter("@" + p.Name, OleDbType.Decimal);
                default:
                    return new OleDbParameter("@" + p.Name, OleDbType.VarChar);
            }
        }

        public static SqlParameter GetSqlParameter(PropertyInfo p)
        {
            switch (p.PropertyType.ToString())
            {
                case "System.DateTime":
                    return new SqlParameter("@" + p.Name, System.Data.SqlDbType.DateTime);
                case "System.Int32":
                    return new SqlParameter("@" + p.Name, System.Data.SqlDbType.Int);
                case "System.Boolean":
                    return new SqlParameter("@" + p.Name, System.Data.SqlDbType.Bit);
                case "System.Double":
                    return new SqlParameter("@" + p.Name, System.Data.SqlDbType.Decimal);
                case "System.Decimal":
                    return new SqlParameter("@" + p.Name, System.Data.SqlDbType.Decimal);
                default:
                    return new SqlParameter("@" + p.Name, System.Data.SqlDbType.VarChar);
            }
        }

        //public static MySql.Data.MySqlClient.MySqlParameter GetMySqlParameter(PropertyInfo p)
        //{
        //    switch (p.PropertyType.ToString())
        //    {
        //        case "System.DateTime":
        //            return new MySql.Data.MySqlClient.MySqlParameter("?" + p.Name, MySql.Data.MySqlClient.MySqlDbType.Datetime);
        //        case "System.Int32":
        //            return new MySql.Data.MySqlClient.MySqlParameter("?" + p.Name, MySql.Data.MySqlClient.MySqlDbType.Int32);
        //        case "System.Boolean":
        //            return new MySql.Data.MySqlClient.MySqlParameter("?" + p.Name, MySql.Data.MySqlClient.MySqlDbType.Bit);
        //        case "System.Double":
        //            return new MySql.Data.MySqlClient.MySqlParameter("?" + p.Name, MySql.Data.MySqlClient.MySqlDbType.Decimal);
        //        case "System.Decimal":
        //            return new MySql.Data.MySqlClient.MySqlParameter("?" + p.Name, MySql.Data.MySqlClient.MySqlDbType.Decimal);
        //        default:
        //            return new MySql.Data.MySqlClient.MySqlParameter("?" + p.Name, MySql.Data.MySqlClient.MySqlDbType.VarChar);
        //    }
        //}

        public static System.Data.SQLite.SQLiteParameter GetSqliteParameter(PropertyInfo p)
        {
            switch (p.PropertyType.ToString())
            {
                case "System.DateTime":
                    return new System.Data.SQLite.SQLiteParameter("@" + p.Name, System.Data.DbType.Date);
                case "System.Int32":
                    return new System.Data.SQLite.SQLiteParameter("@" + p.Name, System.Data.DbType.Int32);
                case "System.Boolean":
                    return new System.Data.SQLite.SQLiteParameter("@" + p.Name, System.Data.DbType.Boolean);
                case "System.Double":
                    return new System.Data.SQLite.SQLiteParameter("@" + p.Name, System.Data.DbType.Decimal);
                case "System.Decimal":
                    return new System.Data.SQLite.SQLiteParameter("@" + p.Name, System.Data.DbType.Decimal);
                default:
                    return new System.Data.SQLite.SQLiteParameter("@" + p.Name, System.Data.DbType.String);
            }
        }
    }
}
